/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.crowtheries.wirelessrouting.messages;

import net.crowtheries.wirelessrouting.utils.Packet;
import net.crowtheries.wirelessrouting.utils.UDPSegment;
import ptolemy.data.IntToken;
import ptolemy.data.RecordToken;
import ptolemy.data.StringToken;
import ptolemy.data.Token;
import ptolemy.kernel.util.IllegalActionException;

/**
 * RREQ Class that generates the RREQ_Token
 * @author knuthy
 */
public class RREQ{
    private RecordToken rreqToken;
    private StringToken SRCIP;
    private StringToken DSTIP;
    private IntToken SRCSEQ;
    private IntToken DSTSEQ;
    private IntToken HOPCOUNT;
    private IntToken RREQID;

    public RREQ(String SRCIP, int SRCSEQ, String DSTIP, int DSTSEQ, int RREQID) throws IllegalActionException{
    	this.SRCIP=new StringToken(SRCIP);
    	this.DSTIP=new StringToken(DSTIP);
    	this.SRCSEQ=new IntToken(SRCSEQ);
        this.DSTSEQ=new IntToken(DSTSEQ);
        this.RREQID=new IntToken(RREQID);
        this.HOPCOUNT=new IntToken(0);
        updateRREQToken();
    }
    
    public RREQ(RecordToken rreq) throws IllegalActionException{ 
        this.DSTIP=(StringToken)rreq.get("DSTIP");
        this.SRCIP=(StringToken)rreq.get("SRCIP");
        this.DSTSEQ=(IntToken)rreq.get("DSTSEQ");
        this.SRCSEQ=(IntToken)rreq.get("SRCSEQ");
        this.RREQID=(IntToken)rreq.get("RREQID");
        this.HOPCOUNT=(IntToken)rreq.get("HOPCOUNT");
        this.updateRREQToken();
    }

    private void updateRREQToken() throws IllegalActionException{
        rreqToken=new RecordToken(new String[]{
                "SRCIP",
                "SRCSEQ",
                "DSTIP",
                "DSTSEQ",
                "RREQID",
                "HOPCOUNT"
            },new Token[]{
                 this.SRCIP,
                 this.SRCSEQ,
                 this.DSTIP,
                 this.DSTSEQ,
                 this.RREQID,
                 this.HOPCOUNT
            });
    }
    public String getSRCIP(){
        return this.SRCIP.stringValue();
    }

    public String getDSTIP(){
        return this.DSTIP.stringValue();
    }

    public int getSRCSEQ(){
        return this.SRCSEQ.intValue();
    }

    public int getDSTSEQ(){
        return this.DSTSEQ.intValue();
    }

    public int getHOPCOUNT(){
        return this.HOPCOUNT.intValue();
    }

    public int getRREQID(){
        return this.RREQID.intValue();
    }

    /**
     * Increments the HOPCOUNT value
     * @throws ptolemy.kernel.util.IllegalActionException
     */
    public void incHop() throws IllegalActionException{
        this.HOPCOUNT=new IntToken(this.HOPCOUNT.intValue()+1);
        updateRREQToken();
    }

    public RecordToken getMessage() throws IllegalActionException{
        return new RecordToken(new String[]{
            "MSGTYPE",
            "MSG"
        },new Token[]{
            new IntToken(1),
            rreqToken
        });
    }

    public RecordToken getRequestToken(String packetSRCIP) throws IllegalActionException{
        RecordToken message=new RecordToken(new String[]{
            "MSGTYPE",
            "MSG"
        },new Token[]{
            new IntToken(1),
            rreqToken
        });
        
        UDPSegment udpSeg=new UDPSegment(654,654,message);
        Packet packet=new Packet("255.255.255.255",packetSRCIP,udpSeg);
        
        return packet.getPacketToken();
    }
    public int hashCode(){
    	return (this.getRREQID()*31)^this.getSRCIP().length();
    }
    public boolean equals(Object o){
    	System.out.println("here!!!");
    	RREQ req=(RREQ)o;
        if(this.getSRCIP().equals(req.getSRCIP()) &&
                this.getRREQID()==req.getRREQID()){
        	System.out.println("xxx");
            return true;
            }
        else{
        	System.out.println(":'(");
            return false;
        }
    }
}
